package com.ruoyi.develop.goodsSaleMaterial.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.factory.factoryProduceSaleAll.domain.FactoryProduceSaleAll;
import com.ruoyi.develop.factory.factoryProduceSaleAll.service.IFactoryProduceSaleAllService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsOrderMaterials.domain.GoodsOrderMaterials;
import com.ruoyi.develop.goodsOrderMaterials.service.IGoodsOrderMaterialsService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaleMaterialChild.domain.GoodsSaleMaterialChild;
import com.ruoyi.develop.goodsSaleMaterialChild.service.IGoodsSaleMaterialChildService;
import com.ruoyi.develop.orderIronDetail.domain.OrderIronDetail;
import com.ruoyi.develop.orderIronDetail.service.IOrderIronDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSaleMaterial.mapper.GoodsSaleMaterialMapper;
import com.ruoyi.develop.goodsSaleMaterial.domain.GoodsSaleMaterial;
import com.ruoyi.develop.goodsSaleMaterial.service.IGoodsSaleMaterialService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售产品物料Service业务层处理
 * @author qh
 * @date 2024-11-09
 */
@Service
public class GoodsSaleMaterialServiceImpl implements IGoodsSaleMaterialService {
    @Resource
    private GoodsSaleMaterialMapper goodsSaleMaterialMapper;
    @Resource
    private IGoodsSaleService goodsSaleService;
    @Resource
    private IOrderIronDetailService orderIronDetailService;
    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;
    @Autowired
    private IFactoryProduceSaleAllService factoryProduceSaleAllService;
    @Autowired
    private IGoodsOrderMaterialsService iGoodsOrderMaterialsService;
    @Autowired
    private IFactoryProduceSaleAllService iFactoryProduceSaleAllService;
    @Autowired
    private IGoodsSaleMaterialChildService goodsSaleMaterialChildService;


    /**
     * 查询销售产品物料
     * @param id 销售产品物料主键
     * @return 销售产品物料
     */
    @Override
    public GoodsSaleMaterial selectById(String id) {
        return goodsSaleMaterialMapper.selectById(id);
    }

    /**
     * 查询销售产品物料列表
     * @param entity 销售产品物料
     * @return 销售产品物料
     */
    @Override
    public List<GoodsSaleMaterial> selectList(GoodsSaleMaterial entity) {
        return goodsSaleMaterialMapper.selectList(entity);
    }

    /**
     * 查询销售产品物料列表
     * @param entity 销售产品物料
     * @return 销售产品物料
     */
    @Override
    public List<GoodsSaleMaterial> goodsSaleMaterialPleaseBuyList(GoodsSaleMaterial entity) {
        return goodsSaleMaterialMapper.goodsSaleMaterialPleaseBuyList(entity);
    }

    /**
     * 新增销售产品物料
     * @param entity 销售产品物料
     * @return 结果
     */
    @Override
    public int insert(GoodsSaleMaterial entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        if(entity.getGoodsCount()!=null) {
            entity.setShengyuCount(entity.getGoodsCount());
        }
        return goodsSaleMaterialMapper.insert(entity);
    }

    /**
     * 修改销售产品物料
     * @param entity 销售产品物料
     * @return 结果
     */
    @Override
    public int update(GoodsSaleMaterial entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return goodsSaleMaterialMapper.update(entity);
    }

    /**
     * 批量删除销售产品物料
     * @param ids 需要删除的销售产品物料主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsSaleMaterialMapper.deleteByIds(ids);
    }

    @Override
    public int deleteHaveGoood(String[] ids) {
        for(String id:ids){
            GoodsSaleMaterial goodsSaleMaterial= goodsSaleMaterialMapper.selectById(id);
            List<GoodsOrderMaterials> goodsOrderMaterialsList = iGoodsOrderMaterialsService.selectByGoodsIdMetailId(goodsSaleMaterial.getMainGoodid(),goodsSaleMaterial.getGoodsId());
            if(goodsOrderMaterialsList.size() > 0){
                for(GoodsOrderMaterials goodsOrderMaterials:goodsOrderMaterialsList){
                    iGoodsOrderMaterialsService.deleteById(goodsOrderMaterials.getId());
                }
            }

        }
        return goodsSaleMaterialMapper.deleteByIds(ids);
    }

    @Override
    public int useGxHaveGoood(String[] ids) {
        for(String id:ids){
            /*GoodsSaleMaterial goodsSaleMaterial= goodsSaleMaterialMapper.selectById(id);
            List<GoodsOrderMaterials> goodsOrderMaterialsList = iGoodsOrderMaterialsService.selectByGoodsIdMetailId(goodsSaleMaterial.getMainGoodid(),goodsSaleMaterial.getGoodsId());
            if(goodsOrderMaterialsList.size() > 0){
                for(GoodsOrderMaterials goodsOrderMaterials:goodsOrderMaterialsList){
                    iGoodsOrderMaterialsService.deleteById(goodsOrderMaterials.getId());
                }
            }*/
            FactoryProduceSaleAll factoryProduceSaleAll=iFactoryProduceSaleAllService.selectById(id);
            List<GoodsOrderMaterials> goodsOrderMaterialsList = iGoodsOrderMaterialsService.selectByGoodsId(factoryProduceSaleAll.getGoodsId());
            for(GoodsOrderMaterials goodsOrderMaterials:goodsOrderMaterialsList){
                GoodsSaleMaterial goodsSaleMaterial=new GoodsSaleMaterial();
                goodsSaleMaterial.setFactoryGoodssaleId(factoryProduceSaleAll.getId());
                goodsSaleMaterial.setGsaleId(factoryProduceSaleAll.getId());
                goodsSaleMaterial.setGoodsId(factoryProduceSaleAll.getGoodsId());
                goodsSaleMaterial.setGoodsCode(goodsOrderMaterials.getGoodsCode());
                goodsSaleMaterial.setGoodsName(goodsOrderMaterials.getGoodsName());
                goodsSaleMaterial.setGoodModel(goodsOrderMaterials.getGoodModel());
                goodsSaleMaterial.setGoodsUnit(goodsOrderMaterials.getGoodsUnit());
                goodsSaleMaterial.setGoodsBrand(goodsOrderMaterials.getGoodsBrand());
                goodsSaleMaterial.setGoodsCount(goodsOrderMaterials.getGoodsCount().multiply(factoryProduceSaleAll.getCount()));
                goodsSaleMaterial.setGoodsBuyPrice(goodsOrderMaterials.getGoodsBuyPrice().multiply(factoryProduceSaleAll.getCount()));
                goodsSaleMaterial.setGoodsBuyTotal(goodsOrderMaterials.getGoodsBuyTotal().multiply(factoryProduceSaleAll.getCount()));
                goodsSaleMaterial.setProjectId(factoryProduceSaleAll.getProjectId());
                goodsSaleMaterial.setProjectName(factoryProduceSaleAll.getGongchengName());
                goodsSaleMaterial.setProjectNo(factoryProduceSaleAll.getProjectNo());
                goodsSaleMaterial.setStatus("0");
                goodsSaleMaterial.setShengyuCount(goodsOrderMaterials.getGoodsCount().multiply(factoryProduceSaleAll.getCount()));
                goodsSaleMaterial.setProduceId(factoryProduceSaleAll.getProduceId());
                goodsSaleMaterial.setFactoryGoodssaleId(factoryProduceSaleAll.getProduceId());
                goodsSaleMaterial.setFactoryProdNo(factoryProduceSaleAll.getProduceNo());
                insert(goodsSaleMaterial);
            }
            if(goodsOrderMaterialsList.size()>0){
                factoryProduceSaleAll.setIschaijie(BusinessConstants.TYPE_CHAIJIE_YES);
                iFactoryProduceSaleAllService.update(factoryProduceSaleAll);
            }

        }
        return 1;
    }

    @Override
    public int deleteMaterial(String[] ids) {
        for(String id:ids){
            GoodsSaleMaterialChild goodsSaleMaterialChild= goodsSaleMaterialChildService.selectById(id);
            GoodsSaleMaterial goodsSaleMaterial=selectById(goodsSaleMaterialChild.getMaterialdId());
            goodsSaleMaterial.setLingquCount(goodsSaleMaterial.getLingquCount().subtract(goodsSaleMaterialChild.getShengyuCount()));
            goodsSaleMaterial.setShengyuCount(goodsSaleMaterial.getShengyuCount().add(goodsSaleMaterialChild.getShengyuCount()));
            update(goodsSaleMaterial);
            goodsSaleMaterialChildService.deleteById(id);
        }
        return 1;
    }

    /**
     * 删除销售产品物料信息
     * @param id 销售产品物料主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSaleMaterialMapper.deleteById(id);
    }

    @Override
    public void deleteBySaleId(String gsaleId) {
        goodsSaleMaterialMapper.deleteBySaleId(gsaleId);
    }

    @Override
    @Transactional
    public void insertBatch(GoodsSaleMaterial goodsSaleMaterial) {
        BigDecimal pleaseBuyCount=goodsSaleMaterial.getGsaleCount();
        List<GoodsOrderIn> rows = goodsSaleMaterial.getColumns();
        if(StringUtils.isNotBlank(goodsSaleMaterial.getGsaleId())){
            FactoryProduceSaleAll factoryProduceSaleAll = factoryProduceSaleAllService.selectById(goodsSaleMaterial.getGsaleId());
            if (!rows.isEmpty()) {
                factoryProduceSaleAll.setIschaijie(BusinessConstants.TYPE_CHAIJIE_YES);
                factoryProduceSaleAllService.update(factoryProduceSaleAll);

                //更新库存管理拆解
               /* GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectById(goodsSale.getGoodsId());
                if(BusinessConstants.TYPE_CHAIJIE_NO.equals(goodsOrderIn.getIschaijie()) || StringUtils.isBlank(goodsOrderIn.getIschaijie())){
                    goodsOrderIn.setIschaijie(BusinessConstants.TYPE_CHAIJIE_YES);
                    goodsOrderInMapper.update(goodsOrderIn);
                }*/

                for (GoodsOrderIn entity : rows) {
                    goodsSaleMaterial =new GoodsSaleMaterial();
                    goodsSaleMaterial.setProjectId(factoryProduceSaleAll.getProjectId());
                    //goodsSaleMaterial.setProjectName(factoryProduceSaleAll.getProjectName());
                    goodsSaleMaterial.setProjectNo(factoryProduceSaleAll.getProjectNo());
                    goodsSaleMaterial.setGsaleId(factoryProduceSaleAll.getId());
                    //goodsSaleMaterial.setSaleName(factoryProduceSaleAll.getSaleName());

                    goodsSaleMaterial.setGoodsId(entity.getId());
                    goodsSaleMaterial.setGoodsCode(entity.getCode());
                    goodsSaleMaterial.setGoodsName(entity.getName());
                    goodsSaleMaterial.setGoodsType(entity.getGoodsType());
                    goodsSaleMaterial.setGoodsBrand(entity.getBrand());
                    goodsSaleMaterial.setGoodsCount(entity.getMateriaCount().multiply(factoryProduceSaleAll.getCount()));
                    goodsSaleMaterial.setGoodModel(entity.getModel());
                    goodsSaleMaterial.setGoodsUnit(entity.getUnit());
                    goodsSaleMaterial.setSupplierId(entity.getSupplierId());
                    goodsSaleMaterial.setSupplierName(entity.getSupplierName());
                    goodsSaleMaterial.setGoodsBuyPrice(entity.getCostPrice());
                    goodsSaleMaterial.setGoodsBuyTotal(entity.getMateriaCount().multiply(entity.getCostPrice()));
                    goodsSaleMaterial.setShengyuCount(entity.getMateriaCount().multiply(factoryProduceSaleAll.getCount()));
                    insert(goodsSaleMaterial);

                    List<GoodsOrderMaterials> goodsOrderMaterialsList = iGoodsOrderMaterialsService.selectByGoodsIdMetailId(factoryProduceSaleAll.getGoodsId(),entity.getId());
                    if(goodsOrderMaterialsList.size() > 0){

                    }else{
                        GoodsOrderMaterials goodsOrderMaterials=new GoodsOrderMaterials();
                        goodsOrderMaterials.setGoodsId(factoryProduceSaleAll.getGoodsId());
                        goodsOrderMaterials.setMaterialId(entity.getId());
                        goodsOrderMaterials.setGoodsCode(entity.getCode());
                        goodsOrderMaterials.setGoodsName(entity.getName());
                        goodsOrderMaterials.setGoodsType(entity.getGoodsType());
                        goodsOrderMaterials.setGoodsBrand(entity.getBrand());
                        goodsOrderMaterials.setGoodModel(entity.getModel());
                        goodsOrderMaterials.setGoodsUnit(entity.getUnit());
                        goodsOrderMaterials.setGoodsCount(entity.getMateriaCount());
                        goodsOrderMaterials.setGoodsBuyPrice(entity.getCostPrice());
                        goodsOrderMaterials.setGoodsBuyTotal(entity.getMateriaCount().multiply(entity.getCostPrice()));
                        goodsOrderMaterials.setRemark(entity.getRemark());
                        iGoodsOrderMaterialsService.insert(goodsOrderMaterials);
                    }

                  /*  //更新库存管理拆解
                    GoodsOrderMaterials goodsOrderMaterials=new GoodsOrderMaterials();
                    goodsOrderMaterials.setGoodsId(goodsSale.getGoodsId());
                    goodsOrderMaterials.setMaterialId(entity.getId());
                    goodsOrderMaterials.setGoodsCode(entity.getCode());
                    goodsOrderMaterials.setGoodsName(entity.getName());
                    goodsOrderMaterials.setGoodsType(entity.getGoodsType());
                    goodsOrderMaterials.setGoodsBrand(entity.getBrand());
                    goodsOrderMaterials.setGoodModel(entity.getModel());
                    goodsOrderMaterials.setGoodsUnit(entity.getUnit());
                    goodsOrderMaterials.setGoodsCount(entity.getMateriaCount());
                    goodsOrderMaterials.setGoodsBuyPrice(entity.getCostPriceTax());
                    goodsOrderMaterials.setGoodsBuyTotal(entity.getCost());
                    goodsOrderMaterials.setRemark(entity.getRemark());
                    insert(goodsOrderMaterials);*/
                }
            }
        }
        if(StringUtils.isNotBlank(goodsSaleMaterial.getIronDetailId())){
            OrderIronDetail orderIronDetail = orderIronDetailService.selectById(goodsSaleMaterial.getIronDetailId());
            if (!rows.isEmpty()) {
                for (GoodsOrderIn entity : rows) {
                    GoodsSaleMaterial material =new GoodsSaleMaterial();
                    material.setProjectId(orderIronDetail.getOrderId());
                    material.setProjectName(orderIronDetail.getOrder().getName());
                    material.setProjectNo(orderIronDetail.getOrder().getProjectCode());
                    material.setIronRecordId(orderIronDetail.getRecordId());
                    material.setIronDetailId(orderIronDetail.getId());
                    material.setSaleName(orderIronDetail.getOrder().getSalesmanName());
                    material.setGoodsId(entity.getId());
                    material.setGoodsCode(entity.getCode());
                    material.setGoodsName(entity.getName());
                    material.setGoodsType(entity.getGoodsType());
                    material.setGoodsBrand(entity.getBrand());
                    material.setGoodsCount(entity.getMateriaCount().multiply(pleaseBuyCount));
                    material.setGoodModel(entity.getModel());
                    material.setGoodsUnit(entity.getUnit());
                    material.setSupplierId(entity.getSupplierId());
                    material.setSupplierName(entity.getSupplierName());
                    material.setGoodsBuyPrice(entity.getCostPrice());
                    material.setGoodsBuyTotal(entity.getMateriaCount().multiply(entity.getCostPrice()));
                    material.setShengyuCount(entity.getMateriaCount().multiply(pleaseBuyCount));
                    insert(material);

                }
            }
        }
    }

    @Override
    public void deleteByIronDetailId(String ironDetailId) {
        goodsSaleMaterialMapper.deleteByIronDetailId(ironDetailId);
    }





}
